#!/bin/bash

$(set -eu)

force=0
# 静默模式
quiet=0
# docker 版本
version=""
# 当前安装的步骤
current_step=""

usage() {
  echo "Usage:"
  echo "-h      Get help"
  echo "-f      Force installation"
  echo "-q      Suppress all normal output"
  echo "Support system:"
  echo "  debian 9~、10~ "
  echo "  centos7.0、7.1、7.2、7.3、7.4、7.9"
  echo "  aliyun"
  echo "Suggest:"
  echo "  centos install fail，please check yum respo"
  echo ""
}

while getopts "hfq" OPT; do
  case $OPT in
  f) force=1 ;;
  q) quiet=1 ;;
  h) usage ;;
  ?) usage ;;
  esac
done

checkVersion() {
  if which docker >/dev/null 2>&1; then
    version=$(docker --version)
    return 0
  fi

  return 1
}

# 静默模式
output() {
  color=$2
  if [ ${#color} -eq 0 ]; then
    color="37"
  fi

  echo -e "\033[${color}m$1\033[0m"
  return 0
}

currentStep() {

  current_step=$1

  i=0
  b=""
  while (("$i" < 100)); do
    i=$(("$i" + 1))
    b="${b}\b\b"
  done

  if [ ${#current_step} -eq 1 ]; then
    printf "${b}%s\n" "Install Completed , $(docker --version)"
    printf "\r"
    return 1
  fi

  if [ $quiet -eq 1 ]; then
    printf "${b}%s" "Current Step: ${current_step}"

  else
    output "Step: ${current_step}"
  fi

  return 0
}

installAptGet() {

  currentStep "remove old version"
  sudo apt-get remove docker docker-engine docker.io containerd runc >/dev/null 2>&1

  currentStep "advanced package tool update"
  sudo apt-get update >/dev/null 2>&1

  currentStep "install extension"
  sudo sudo apt-get -y install ca-certificates curl gnupg lsb-release >/dev/null 2>&1

  sudo mkdir -p /etc/apt/keyrings
  if [ ! -f "/etc/apt/keyrings/docker.gpg" ]; then
    curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  fi

  $(echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null)

  currentStep "start install docker"
  sudo apt-get update >/dev/null 2>&1
  sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin >/dev/null

  if [ $? -eq 0 ]; then
    currentStep 1
  fi
}

installYum() {

  currentStep "remove old version"
  sudo yum remove docker \
    docker-client \
    docker-client-latest \
    docker-common \
    docker-latest \
    docker-latest-logrotate \
    docker-logrotate \
    docker-engine >/dev/null 2>&1

  currentStep "install yum-utils"
  sudo yum install -y yum-utils >/dev/null 2>&1

  sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo >/dev/null 2>&1

  # centos 8 系列安装的时候报错 http://www.manongjc.com/detail/22-zhlhkpnlseikjgr.html
  version8=$(echo "${operate_system}" | grep 8)
  allow_erasing=""
  if [ ${#version8} -gt 0 ]; then
    allow_erasing="--allowerasing"
  fi

  currentStep "start install docker"
  sudo yum install -y ${allow_erasing} docker-ce docker-ce-cli containerd.io docker-compose-plugin >/dev/null

  if [ $? -eq 0 ]; then
    currentStep 1
  fi
}

checkVersion

if [[ $? -eq 1 || $force -eq 1 ]]; then

  commands=("yum" "apt-get")

  for i in "${commands[@]}"; do
    if which "$i" >/dev/null 2>&1; then
      if [ "$i" = "yum" ]; then
        echo "Install docker by $i command"
        installYum
      elif [ "$i" = "apt-get" ]; then
        echo "Install docker by $i command"
        installAptGet
      fi
    fi
  done

else
  echo -e "\033[31mDocker has been installed\033[0m"
  echo -e "\033[32m${version}\033[0m"
  echo -e "\033[37mIf you want to force the installation,you can use -f parameter\033[0m"
fi
